using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SpoilsInventory
{
    public partial class GridColumnOrder : Form
    {

        string mFormName = "";
        DataGridView mGrid;
        Dictionary<string,DataGridViewColumn> mGridColumns = new Dictionary<string,DataGridViewColumn>();

        public GridColumnOrder(string formName, DataGridView theGrid)
        {
            InitializeComponent();

            mFormName = formName;
            mGrid = theGrid;
        }

        private void GridColumnOrder_Load(object sender, EventArgs e)
        {
            string aGridColumnSetting = Common.GetAppSetting(Application.StartupPath, mFormName + "." + mGrid.Name + ".ColumnOrder");
            

            if(aGridColumnSetting != "")
            {
                string[] aGridColumns = aGridColumnSetting.Split(";".ToCharArray());
                
                for (int i = 0; i < aGridColumns.Length; i++)
                {
                    if (aGridColumns[i] != "")
                    {
                        GridColumnListBox.Items.Add(aGridColumns[i]);
                        mGridColumns.Add(aGridColumns[i], mGrid.Columns[aGridColumns[i]]);
                    }
                }
            }

            foreach (DataGridViewColumn aColumn in mGrid.Columns)
            {
                if(!GridColumnListBox.Items.Contains(aColumn.DataPropertyName))
                {
                    GridColumnListBox.Items.Add(aColumn.DataPropertyName);
                    mGridColumns.Add(aColumn.DataPropertyName, aColumn);
                }
            }


        }

        private void MoveUpButton_Click(object sender, EventArgs e)
        { 
            string aSelectedColumn = GridColumnListBox.SelectedItem.ToString();
            string aPreviousColumn = GridColumnListBox.Items[GridColumnListBox.SelectedIndex - 1].ToString();

            GridColumnListBox.Items[GridColumnListBox.SelectedIndex - 1] = aSelectedColumn;
            GridColumnListBox.Items[GridColumnListBox.SelectedIndex] = aPreviousColumn;

            GridColumnListBox.SelectedIndex -= 1;
        }

        private void MoveDownButton_Click(object sender, EventArgs e)
        {
            string aSelectedColumn = GridColumnListBox.SelectedItem.ToString();
            string aNextColumn = GridColumnListBox.Items[GridColumnListBox.SelectedIndex + 1].ToString();

            GridColumnListBox.Items[GridColumnListBox.SelectedIndex + 1] = aSelectedColumn;
            GridColumnListBox.Items[GridColumnListBox.SelectedIndex] = aNextColumn;

            GridColumnListBox.SelectedIndex += 1;
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            //mGrid.Columns.Clear();
            string aColumns = "";
            for (int i = 0; i < GridColumnListBox.Items.Count; i++)
            {
                //mGrid.Columns.Add(mGridColumns[GridColumnListBox.Items[i].ToString()]);
                mGrid.Columns[GridColumnListBox.Items[i].ToString()].DisplayIndex = i;
                aColumns += GridColumnListBox.Items[i].ToString() + ";";
            }

            Common.SetAppSetting(Application.StartupPath, mFormName + "." + mGrid.Name + ".ColumnOrder", aColumns.Substring(0, aColumns.Length - 1));
            
            this.Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}